package com.furentech.iotserver.fa.sinopec;

import com.furentech.iotserver.fa.sinopec.sinopec_processor.*;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Hashtable;

@Component
public class SinopecPackageProcessor implements IProtocolParser{

    private final static Logger logger = LoggerFactory.getLogger(SinopecPackageProcessor.class);
    Hashtable<Byte, IProtocolParser> processorTable;

    @Autowired
    Poll_Processor poll_processor;
    @Autowired
    NozzleStData_Processor nozzleStData_processor;
    @Autowired
    TranData_Processor tranData_processor;
    @Autowired
    AuthReq1_Processor authReq1_processor;
    @Autowired
    AuthReq2_Processor authReq2_processor;
    @Autowired
    WBListCheck_Processor wbListCheck_processor;
    @Autowired
    NozzleDownData_Processor nozzleDownData_processor;
    @Autowired
    ReqCont_Processor reqCont_processor;
    @Autowired
    ReqData_Processor reqData_processor;
    @Autowired
    ReqSignIn_Processor reqSignIn_processor;
    @Autowired
    ReqOnlineAuth_Processor reqOnlineAuth_processor;
    @Autowired
    ReqDebit_Processor reqDebit_processor;
    @Autowired
    ReqReverseAuth_Processor reqReverseAuth;
    @Autowired
    ReqCustomBal_Processor reqCustomBal_Processor;

    @Override
    public String getJsonString(byte[] buf) {

        if(this.processorTable.containsKey(buf[0]))
        {
            return processorTable.get(buf[0]).getJsonString(buf);
        }
        logger.warn(String.format("未找到功能码为0x%02x的解析器",buf[0]));
        return null;
    }

    @Override
    public String getReadableString(byte[] buf) {

        if(this.processorTable.containsKey(buf[0]))
        {
            return processorTable.get(buf[0]).getReadableString(buf);
        }

        logger.warn(String.format("未找到功能码为0x%02x的解析器",buf[0]));
        return null;
    }

    @Override
    public byte[] getResponse(byte[] buf, Channel channel) {
        if(this.processorTable.containsKey(buf[0]))
        {
            return processorTable.get(buf[0]).getResponse(buf,channel);
        }
        logger.warn(String.format("未找到功能码为0x%02x的解析器",buf[0]));
        return null;
    }

    public SinopecPackageProcessor()
    {
        processorTable =new Hashtable<Byte, IProtocolParser>();
    }

    public void parserInit()
    {
        processorTable.put(SinopecFunCode.Poll,poll_processor);
        processorTable.put(SinopecFunCode.Nozzle_St_Data, nozzleStData_processor);
        processorTable.put(SinopecFunCode.Tran_Data, tranData_processor);
        processorTable.put(SinopecFunCode.Auth_Req1, authReq1_processor);
        processorTable.put(SinopecFunCode.Auth_Req2, authReq2_processor);
        processorTable.put(SinopecFunCode.BWList_check, wbListCheck_processor);
        processorTable.put(SinopecFunCode.Nozzle_Down_Data, nozzleDownData_processor);
        processorTable.put(SinopecFunCode.Req_Data, reqData_processor);
        processorTable.put(SinopecFunCode.Req_Cont, reqCont_processor);
        processorTable.put(SinopecFunCode.Req_Sign_in, reqSignIn_processor);
        processorTable.put(SinopecFunCode.Req_Online_Auth, reqOnlineAuth_processor);
        processorTable.put(SinopecFunCode.Req_Debit, reqDebit_processor);
        processorTable.put(SinopecFunCode.Req_Reverse_Auth, reqReverseAuth);
        processorTable.put(SinopecFunCode.Req_Custom_Bal, reqCustomBal_Processor);
    }
}
